/**
 * Copyright (c) 2012 Nokia Corporation.
 */

package com.nokia.example.simplepurchase.uicomponents;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * The step indicator for touch and type and full touch Series 40 devices.
 */
public class StepIndicatorHi extends AbstractStepIndicator {
	// Constants
	private static final int MARGIN = 6;
	private static final int STEP_IMAGE_SIZE = 18;
	private static final int HEIGHT = STEP_IMAGE_SIZE + MARGIN * 2;
	
	// Members
	private Image _backgroundImage = null;
	private Image _stepImage = null;
	private Image _currentStepImage = null;
	
	/**
	 * Constructor.
	 */
	public StepIndicatorHi(int width, int stepCount) {
		super(width, HEIGHT, stepCount);
		
		try {
			_backgroundImage = Image.createImage("/graphics/common/step-indicator-bg.png");
			_stepImage = Image.createImage("/graphics/common/step.png");
			_currentStepImage = Image.createImage("/graphics/common/current-step.png");
		}
		catch (IOException ex) {
			System.out.println("Failed to load step image!");
		}
	}
	
	/**
	 * From CanvasGraphicsItem.
	 */
	public void paint(Graphics graphics) {
		// Draw background
		graphics.drawImage(_backgroundImage, 0, 0, Graphics.TOP | Graphics.LEFT);
		
		// Draw step images
		Image imageToDraw = null;
		final int startX =
				(_width - (STEP_IMAGE_SIZE * _stepCount
						+ MARGIN * (_stepCount - 1))) / 2;
		
		for (int i = 0; i < _stepCount; ++i) {
			if (_currentStep == i + 1) {
				imageToDraw = _currentStepImage;
			}
			else {
				imageToDraw = _stepImage;
			}
			
			graphics.drawImage(imageToDraw, startX + i * (STEP_IMAGE_SIZE + MARGIN),
							   MARGIN, Graphics.TOP | Graphics.LEFT);
		}
	}
}
